<?php
include '../../init.php';

// 获取板块名称
$page    = @$_GET['page'] - 1;
if ($page < 0){
	$page = 0;
}
$count = @$_GET['count'];
if (!$count) {
	$count = 10;
}


// 获取当前板块的名称
$data = array();
$modulID = @$_GET['zid'];
$sql = "SELECT title FROM `zone_modul_tb` WHERE zid={$modulID}";
my_mysql_query($sql,$data);
$title = $data[0]['title'];

// 获取版主姓名
$sql = "select u.alias as alias,u.uid as uid from relo_tb as r,userinfo_tb as u where modulid={$modulID} and u.uid=r.uid AND status=1; ";
$roleArray = array();
my_mysql_query($sql, $roleArray);

// 当前用户是否是版主
$isModerator = false;
$uid = @$_SESSION['uid'];
if (!empty($uid)){
	foreach($roleArray as $role){
		 if($role['uid'] == $uid) {
		 	$isModerator = true;
		 	break;
		 }
	}	
}


// 获取当前登录用户是否有发言权限,没有登录就没有权限
$isStop = (bool)@$_SESSION['isStop'];


// 获取帖子列表,帖子状态,获取帖子作者,最后一次回复,已时间列表排序
$datas  = array();
$fields = "SELECT pid, modid, uid, title, isTop, iswally, create_time, (SELECT alias FROM `userinfo_tb` AS u WHERE u.uid=p.uid ) AS alias ";
$table  = "FROM `post_tb` AS p ";
$where  = "WHERE p.parentId=0 AND isDel=0 AND isTop=0 AND modid={$modulID} ORDER BY create_time desc ";
$limit  = "LIMIT ".($page * $count).",{$count};";
$sql  = $fields.$table.$where.$limit;
$tip  = '';
$ret  = my_mysql_query($sql, $datas);
if (!$ret && $page == 0){
	$tip = '目前该板块还没有帖子';
} elseif (count($datas) < $count){
	$tip = '已经没有更多帖子了';
}

// 获取帖子总数
$sql = "SELECT count(pid) as count FROM `post_tb` WHERE parentId=0 AND isDel=0 AND isTop=0 AND modid=".$modulID;
$array = array();
my_mysql_query($sql, $array);


// 获取顶置贴
$wallyDatas = array();
$where = "WHERE p.parentId=0 AND isDel=0 AND isTop=1 AND modid={$modulID} ORDER BY create_time desc ";
$sql   = $fields.$table.$where;
my_mysql_query($sql, $wallyDatas);

?>
<!DOCTYPE html>

<html>
	<head>
	    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	    <title><?php echo $title; ?></title>
	    <meta name="keywords" content="">
	    <meta name="description" content="">
	</head>
	<style type="text/css">
	* {
		margin: 0;
		padding: 0;
		list-style: none;
		box-sizing: border-box;
	}
	a:link {
		color: rgb(130,130,130);
		text-decoration: none;
	}
	a:hover {
		color: orange;
		text-decoration: underline;
	}
	.mainbody {
		width: 960px;
		margin: 0 auto;
		background: url(./image/background.png) 0 0 repeat;
		border: 1px solid rgb(230,230,230);
	}
	.head-info {
		height: 200px;
		width: 100%;
		padding: 0 5px;
	}
	.list_top_item {
		margin-bottom: 50px;
	}
	.post-list {
		width: 100%;
		padding: 0 5px;
	}
	.toolbar {
		height: 48px;
		line-height: 48px;
		color: rgb(130,130,130);
		font-size:13px;
		width: 100%;
	}
	.content-list {
		width: 100%;
	}
	.shortcut-post {
		width: 100%;
		margin: 0 5px;
		min-height: 200px;
		box-sizing: border-box;
		padding: 15px;
	}
	.list_item{
		width: 100%;
		padding: 5px 15px;
		background: white;
		box-sizing: border-box;
	}
	.list_item div{
		height: 32px;
		line-height: 32px;
		display: inline-block;
	}
	.list_item .list_item_title{
		width: 450px;
		overflow: hidden;
	}
	.list_item .list_item_creater{
		width: 100px;
	}
	.list_item .list_item_time{
		width: 160px;
	}
	.list_item .list_item_option{
		width: 140px;
	}
	.point-red{
		color: red;
	}
	.point-gold{
		color: gold;
	}
	form{
		display: inline-block;
	}
	.pnc, a.pnc {
	    border-color: #c4c4c4;
	    border-radius: 4px;
	}
	.pn {
	    vertical-align: middle;
	    overflow: hidden;
	    margin-right: 3px;
	    padding: 0;
	    height: 23px;
	    border: 1px solid #999;
	    background: #E5E5E5 url(http://image.gfan.com/static/image/gfan/pn.png) repeat-x 0 0;
	    cursor: pointer;
	    -moz-box-shadow: 0 1px 0 #E5E5E5;
	    -webkit-box-shadow: 0 1px 0 #E5E5E5;
	    box-shadow: 0 1px 0 #E5E5E5;
	}
	</style>
	<body>
		<div class="mainbody">
			<div class="head-info">
				<?php
					echo '<span style="margin:15px 5px; font-size:13px; color:rgb(130,130,130);">'.$tip.'</span>';
				?>
				<p style="height:32px;line-height:32px;padding-left:20px;background-color:rgb(96,137,188);">
					<a href="../../index.php">首页</a>
				</p>
                <br />
				<p style="font-size:24px;color:rgb(130,130,130);"><?php echo $title; ?></p>
                <br />
                <span style="color:rgb(180,180,180);">版主信息:</span>
                <?php
                if (count($roleArray)<1){
                    echo '还没有版主';
                }else{
                    foreach($roleArray as $role){
                        echo '&nbsp;<a href="../user/index.php?uid='.$role['uid'].'">'.$role['alias'].'</a>';
                    }   
                }
                ?>
			</div>

            <a href="moderator.php?zid=<?php echo $modulID; ?>" style="font-size:12px;text-decoration:underline">版主申请&gt;&gt;</a>
            <br /><br />
			<!-- 显示顶置的帖子 -->
			<div class="list_top_item">

					<div class="list_item">
						<div class="list_item_title">标题</div>
						<div class="list_item_creater">作者</div>
						<div class="list_item_time">时间</div>
						<?php
						// 如果是管理员,就显示加精和顶置的操作按钮
						if (@$_SESSION['isAdmin'] || $isModerator){
							echo '<div class="list_item_option">操作</div>';
						}
						?>
					</div>

					<?php
					// 顶置的帖子
					foreach($wallyDatas as $wally){
					?>

					<div class="list_item">
						<div class="list_item_title">
						<!-- 连接到帖子列表 -->
						<a href="../post_list/index.php?zid=<?php echo $modulID; ?>&pid=<?php echo $wally['pid'];?>">
							<?php
							if ($wally['iswally']) {
								echo '<span class="point-red">[加精]</span>';
							}
							?>
							<?php echo $wally['title']; ?>
						</a>
						</div>
						<div class="list_item_creater">
							<a href="../user/index.php?uid=<?php echo $wally['uid']?>">
							<?php echo $wally['alias']; ?>
							</a>
						</div>
						<div class="list_item_time"><?php echo date('Y-m-d H:i:s', $wally['create_time']) ?></div>

						<?php
						// 如果是管理员,就显示取消顶置的操作按钮
						if (@$_SESSION['isAdmin'] || $isModerator){
						echo '<div class="list_item_option">';
						?>
							<form action="post.php" method="POST">
								<input type="hidden" name="pid" value="<?php echo $wally['pid'];?>">
								<input type="hidden" name="uid" value="<?php echo $wally['uid'];?>">
								<input type="hidden" name="value" value="0">
								<button type="submit" name="action" value="updateTop">取消顶置</button>
							</form>
						<?php
						echo '</div>';
						}
						?>
					</div>
					<?php
					}
					?>
			</div>


			<div class="post-list">
				<div class="toolbar">
					<form action="index.php" method="GET">
						<input type="hidden" name="zid" value="<?php echo $modulID; ?>">
						<input type="hidden" name="title" value="<?php echo $title; ?>">
						共有<?php echo ceil($array[0]['count']/$count);?>页,每页有
						<input type="text" size="3" name="count" value="<?php echo $count; ?>">帖子
						当前第<?php echo ($page+1);?>页,跳到第
						<input type="text" size="3" name="page" value="<?php echo $page+2; ?>">页
						<input type="submit" value="GO">
					</form>
				</div>
				<div class="content-list">

					<div class="list_item">
						<div class="list_item_title">标题</div>
						<div class="list_item_creater">作者</div>
						<div class="list_item_time">时间</div>
						<?php
						// 如果是管理员,就显示加精和顶置的操作按钮
						if (@$_SESSION['isAdmin'] || $isModerator){
							echo '<div class="list_item_option">操作</div>';
						}
						?>
					</div>

					<?php
					foreach($datas as $value){
					?>

					<div class="list_item">
						<div class="list_item_title">
							<!-- 连接到帖子列表 -->
							<a href="../post_list/index.php?zid=<?php echo $modulID; ?>&pid=<?php echo $value['pid'];?>">
								<?php
								if ($value['iswally']) {
									echo '<span class="point-red">[加精]</span>';
								}
								?>
								<?php echo $value['title']; ?>
							</a>
						</div>
						<div class="list_item_creater">
							<a href="../user/index.php?uid=<?php echo $value['uid'] ?>"><?php echo $value['alias']; ?></a>
						</div>
						<div class="list_item_time"><?php echo date('Y-m-d H:i:s', $value['create_time']) ?></div>


						<?php

						// 如果是管理员,就显示加精和顶置的操作按钮
						if (@$_SESSION['isAdmin'] || $isModerator){

							echo '<div class="list_item_option">';

							// 已经加精了
							if (false == $value['iswally']) {
							?>
								<form action="post.php" method="POST">
									<input type="hidden" name="pid" value="<?php echo $value['pid'];?>">
									<input type="hidden" name="uid" value="<?php echo $value['uid'];?>">
									<input type="hidden" name="value" value="1">
									<button type="submit" name="action" value="updateGood">加精</button>
								</form>
							<?php
							} else {
							?>
								<form action="post.php" method="POST">
									<input type="hidden" name="pid" value="<?php echo $value['pid'];?>">
									<input type="hidden" name="uid" value="<?php echo $value['uid'];?>">
									<input type="hidden" name="value" value="0">
									<button type="submit" name="action" value="updateGood">撤销精贴</button>
								</form>
							<?php
							}
							if (false == $value['isTop']) {
							?>
								<form action="post.php" method="POST">
									<input type="hidden" name="pid" value="<?php echo $value['pid'];?>">
									<input type="hidden" name="uid" value="<?php echo $value['uid'];?>">
								<input type="hidden" name="value" value="1">
									<button type="submit" name="action" value="updateTop">顶置</button>
								</form>
							<?php
							}
							?>

							<form action="post.php" method="POST">
								<input type="hidden" name="pid" value="<?php echo $value['pid'];?>">
								<button type="submit" name="action" value="addDel">删除</button>
							</form>

						<?php
							echo '</div>';
						}
						?>

					</div>

					<?php
					}
					?>

				</div>
				<div class="toolbar">

					<form action="index.php" method="GET">
						<input type="hidden" name="zid" value="<?php echo $modulID; ?>">
						<input type="hidden" name="title" value="<?php echo $title; ?>">
						共有<?php echo ceil($array[0]['count']/$count);?>页,每页有
						<input type="text" size="3" name="count" value="<?php echo $count; ?>">帖子
						当前第<?php echo ($page+1);?>页,跳到第
						<input type="text" size="3" name="page" value="<?php echo $page+2; ?>">页
						<input type="submit" value="GO">
					</form>

				</div>
			</div>
			<div class="shortcut-post">

				<script charset="utf-8" src="../../plugins/kindeditor/kindeditor-min.js"></script>
				<script charset="utf-8" src="../../plugins/kindeditor/lang/zh_CN.js"></script>
				<script>
					var editor;
					KindEditor.ready(function(K) {
						editor = K.create('#editor-id', {
							resizeType : 0,
							allowPreviewEmoticons : false,
							allowImageUpload : false,
							items : [
								'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
								'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
								'insertunorderedlist', '|', 'emoticons', 'image', 'link']
						});
					});
				</script>
				<!-- -->
				<p style="margin-bottom: 15px;color:rgb(130,130,130);">快捷发帖</p>
				<form action="post.php" onsubmit="editor.sync(); return true;" method="POST">
					<input type="hidden" name="action" value="post">
					<input type="text" name="title" value="" style="outline:none;width:300px;height:24px"><br /><br />
					<textarea id="editor-id" name="content" style="width:700px;height:200px;display:none;"></textarea><br />
					<?php
					if (@$_SESSION['isStop']) {
						echo '<span>你被禁言了</span>';
					} else if (@$_SESSION['is_login']) {
						echo '<button type="submit" name="modulID" value="'.$modulID.'">快捷发帖</button>';
					} else {
						echo '<span style="font-size:13px;color:rgb(130,130,130)">'.
							'<a href="../login/index.php">登录才能发帖</a></span>';
					}
					?>
				</form>
			</div>
		</div>
	</body>
</html>
